import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Demo1 {

    private DatagramSocket socket = null;
    private String serverip;
    private int serverport;

    public Demo1(String s, int p) throws SocketException {
        serverip = s;
        serverport = p;
        socket = new DatagramSocket();
    }

    public void start() throws IOException {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入请求：");
            if (!scanner.hasNext()) {
                break;
            }

            String s1 = scanner.next();

            DatagramPacket p1 = new DatagramPacket(s1.getBytes(), 0, s1.getBytes().length, InetAddress.getByName(serverip),serverport);

            socket.send(p1);

            DatagramPacket p2 = new DatagramPacket(new byte[4096], 4096);
            socket.receive(p2);
            String s = new String(p2.getData(), 0, p2.getLength());
            System.out.println(s);
        }

    }

    public static void main(String[] args) throws IOException {
        Demo1 d = new Demo1("127.0.0.1", 9099);
        d.start();
    }
}
